#!/usr/bin/env bash
set -eu

version=3.4.2
arch="$(uname -m)"

case $arch in
    "x86_64")
        binary="shfmt_v${version}_linux_amd64"
        sha256=9cc743f058ab8896ca6fa94a7f2e570b95294e8a142600f09fe832e406a88f18
        ;;

    "aarch64")
        binary="shfmt_v${version}_linux_arm64"
        sha256=93852ee7d64389802a65c9e58840eebcae43c771d7dcd73d2e25b8b0b87e7966
        ;;
esac

check_version() {
    out="$(shfmt --version 2>/dev/null)" && [ "$out" = "v$version" ]
}

if ! check_version; then
    tmpdir="$(mktemp -d)"
    trap 'rm -r "$tmpdir"' EXIT
    cd "$tmpdir"
    curl -fLO "https://github.com/mvdan/sh/releases/download/v$version/$binary"
    sha256sum -c <<<"$sha256 $binary"
    chmod +x "$binary"
    mv "$binary" /usr/local/bin/shfmt
    check_version
fi
